package com.henchman.samples;

import org.tigris.subversion.javahl.ChangePath;

import java.util.Map;
import java.util.Date;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 */
public class SvnRevisionInfo {
    private static final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS z");

    public final static String[] STD_PROPS = new String[] {"svn:date", "svn:author", "svn:log"};
    private long revisionNumber;
    private Map<String, String> revProps;
    private ChangePath[] changedPaths;

    public SvnRevisionInfo(long revisionNumber, Map revProps, ChangePath[] changedPaths) {
        this.changedPaths = changedPaths;
        this.revisionNumber = revisionNumber;
        this.revProps = revProps;
    }

    public long getRevisionNumber() {
        return revisionNumber;
    }

    public String getAuthor() {
        return revProps.get("svn:author");
    }

    public String getMessage() {
        return revProps.get("svn:log");
    }

    public ChangePath[] getChangedPaths() {
        return changedPaths;
    }

    public Date getDate() {
        try {
            String dateString = revProps.get("svn:date");

            if (dateString == null || dateString.length() != 27 || dateString.charAt(26) != 'Z')
            {
                throw new ParseException("String is not a valid Subversion date", 0);
            }
            synchronized (formatter) {
                return formatter.parse(dateString.substring(0, 23) + " UTC");
            }
        } catch (ParseException e) {
            return null;
        }
    }
}
